<template>
  <div ref="chartContainer" class="chart-container" style="height: 250px"></div>
</template>

<script>
import echarts from 'echarts'

export default {
  props: {
    options: {
      type: Object,
      required: true,
    },
    theme: {
      type: String,
      default: '',
    },
    resize: {
      type: Boolean,
      default: false,
    },
  },
  mounted () {
    this.renderChart()
    if (this.resize) {
      window.addEventListener('resize', this.handleResize)
    }
  },
  beforeDestroy () {
    if (this.resize) {
      window.removeEventListener('resize', this.handleResize)
    }
    this.disposeChart()
  },
  methods: {
    renderChart () {
      const chartContainer = this.$refs.chartContainer
      this.chart = echarts.init(chartContainer)
      this.chart.setOption(this.options)
    },
    disposeChart () {
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    },
    handleResize () {
      if (this.chart) {
        this.chart.resize()
      }
    },
  },
};
</script>

<style scoped>
.chart-container {
  height: 250px;
}
</style>
